<template>
  <div class="com-container" @dblclick="revertMap">
      <div class="com-chart" ref="map_ref"></div>
  </div>
</template>

<script>
import { mapState } from 'vuex';
import axios from 'axios';
import { getProvinceMapInfo } from '../utils/map_utils';
export default {
    data () {
        return {
            chartInstance: null,
            allData: null,
            mapData: {}
        }
    },
    created() {
        this.$socket.registerCallBack('mapData', this.getData);
    },
    mounted () {
        this.initChart();
        // this.getData();
        this.$socket.send({
            action: 'getData',
            socketType: 'mapData',
            chartName: 'map',
            value: ''
        })
        window.addEventListener('resize', this.screenAdapter);
        this.screenAdapter()
    },
    destroyed () {
        window.removeEventListener('resize', this.screenAdapter);
        this.$socket.unRegisterCallBack('mapData');
    },
    methods: {
        async initChart () {
            this.chartInstance = this.$echarts.init(this.$refs.map_ref, this.theme);
            const ret = await axios.get("http://localhost:8888/public/static/map/china.json#/");
            this.$echarts.registerMap('china', ret.data);
            const initOption = {
                title: {
                    text: "▎商家分布",
                    top: 20,
                    left: 20
                },
                legend: {
                    left: '5%',
                    bottom: '5%',
                    orient: 'vertical'
                },
                geo: {
                    type: 'map',
                    map: 'china',
                    top: '5%',
                    bottom: '5%',
                    itemStyle: {
                        areaColor: '#2E72BF',
                        borderColor: '#333'
                    }
                }
            };
            this.chartInstance.setOption(initOption);
            this.chartInstance.on('click', async arg => {
                const provinceInfo = getProvinceMapInfo(arg.name);
                if (!this.mapData[provinceInfo.key]) {
                    const provinceMap = await axios.get('http://localhost:8888/public' + provinceInfo.path);
                    this.mapData[provinceInfo.key] = provinceMap.data;
                    this.$echarts.registerMap(provinceInfo.key, provinceMap.data);
                }
                const changeOption = {
                    geo: {
                        map: provinceInfo.key
                    }
                }
                this.chartInstance.setOption(changeOption);
            })
        },
        getData (ret) {
            // const {data: ret} = await this.$http.get('map');
            this.allData = ret;
            this.updateChart()
        },
        updateChart () {
            const legendArr = this.allData.map(item => {
                return item.name;
            })
            const seriesArr = this.allData.map(item => {
                return {
                    type: 'effectScatter',
                    rippleEffect: {
                        scale: 5,
                        brushType: 'stroke'
                    },
                    name: item.name,
                    data: item.children,
                    coordinateSystem: 'geo'
                }
            })
            const dataOption = {
                legend: {
                    data: legendArr
                },
                series: seriesArr
            };
            this.chartInstance.setOption(dataOption);
        },
        screenAdapter () {
            const titleFontSize = this.$refs.map_ref.offsetWidth / 100 * 3.6;
            const adapterOption = {
                title: {
                    textStyle: {
                        fontSize: titleFontSize
                    }
                },
                legend: {
                    itemWidth: titleFontSize / 2,
                    itemHeight: titleFontSize / 2,
                    itemGap: titleFontSize / 2,
                    textStyle: {
                        fontSize: titleFontSize / 2
                    }
                }
            };
            this.chartInstance.setOption(adapterOption);
            // 手动调用图表对象的resize才能产生效果
            this.chartInstance.resize()
        },
        revertMap () {
            const revertOption = {
                geo: {
                    map: 'china'
                }
            }
            this.chartInstance.setOption(revertOption)
        }
    },
    computed: {
        ...mapState(['theme'])
    },
    watch: {
        theme () {
            this.chartInstance.dispose(); // 销毁当前图表
            this.initChart();
            this.screenAdapter();
            this.updateChart();
        }
    }
}
</script>

<style>

</style>